package tzg.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(urlPatterns = {"/init.Servlet"})
public class InitServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Cookie[] cookies=request.getCookies();
        boolean flag=false;
        String name="";
        if (null!=cookies){
            for(Cookie cookie:cookies){
                if(cookie.getName().equals("firstCookie")){
                    name=cookie.getValue();
                    flag=true;
                    break;
                }
            }
        }

        if (flag==true){
            HttpSession session=request.getSession();
            session.setAttribute("name",name);
            request.getRequestDispatcher("view/show.jsp").forward(request,response);
        }else {
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }


    }
}
